package pos;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import java.util.ArrayList;
import java.util.List;

import adapter.PosAuthRecordAdapter;
import base.BaseAct;
import base.BaseBiz;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.poscontrol.beanmodel.PosAuthRecordBean;
import ui.activity.profit.biz.ProfitBiz;


/**
 * Created by fangjunsong on 19/3/19
 * pos申请记录
 */

public class PosAutoRecordAct extends BaseAct {


    ProfitBiz biz;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    @BindView(R.id.pushpeople_list)
    RecyclerViewFinal rv;
    List<PosAuthRecordBean.RecordsBean> databeans = new ArrayList<>();
    PosAuthRecordAdapter adp;
    private int page=0;
    private int size = 100;
    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_posauthrecord);

    }
    @Override
    protected void setup() {
        setToolBar("申请记录", "");
    }
    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        biz = new ProfitBiz();
        rv.setLayoutManager(new LinearLayoutManager(PosAutoRecordAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());

        getData(true);

        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getData(true);
            }
        });
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                getData(false);
            }
        });
    }
    private void getData(boolean isrefresh){
        if(isrefresh){
            databeans.clear();
            page=1;
        }else{
            page+=1;
        }

        biz.GetPosAuthRecord(page,size,new BaseBiz.Callback<PosAuthRecordBean>() {
            @Override
            public void onSuccess(PosAuthRecordBean pushBackPeopleBean) {

                if (isrefresh) {
                    ptr.onRefreshComplete();
                } else {
                    rv.onLoadMoreComplete();
                }
                databeans.addAll(pushBackPeopleBean.getRecords());
                if(null == adp){
                adp = new PosAuthRecordAdapter(PosAutoRecordAct.this,databeans);
                rv.setAdapter(adp);
                }else{
                    adp.notifyDataSetChanged();
                }
                if(size==pushBackPeopleBean.getRecords().size()) {
                    rv.setHasLoadMore(true);
                }else{
                    rv.setHasLoadMore(false);
                }
            }

            @Override
            public void onFailure(PosAuthRecordBean pushBackPeopleBean) {
                ptr.onRefreshComplete();
            }
        });
    }
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {
    }
    @Override
    protected void onStop() {
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        TobuyApplication.piliangPosition.clear();
    }

}
